package com.RedRiver.hospitalsystem.personal_service.app.exception;

import com.RedRiver.hospitalsystem.exception.RedRiverHISException;
import com.RedRiver.hospitalsystem.result.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class PersonalExceptionHandle {

    @ExceptionHandler(RedRiverHISException.class)
    public Result<?> HandlerException(RedRiverHISException e){
        return Result.getInstance().setCode(e.getCode()).setMsg(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result HandlerException(Exception e){
        e.printStackTrace();
        return Result.getInstance().setCode(500).setMsg("服务器繁忙");
    }

    @ExceptionHandler(BindException.class)
    public Result handlerBindException(BindException bi){
        ObjectError objectError = bi.getAllErrors().get(0);
        System.out.println(bi.getAllErrors());
        return Result.getInstance().setCode(500).setMsg(objectError.getDefaultMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handlerBindException(MethodArgumentNotValidException e){
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        System.out.println(e.getBindingResult().getAllErrors());
        return Result.getInstance().setCode(500).setMsg(objectError.getDefaultMessage());
    }

}
